import UcRequest, { UcResponse } from './ucRequest'

/**
 * 检查用户名
 * 
 * 注册账户前需要先检查用户名是否存在
 */
export default class UserCheckUsernameRequest extends UcRequest
{
    module = 'user'
    action = 'check_username'
    httpMethod = 'get'
    constructor(username){
        super()
        this.params = {
            username: username
        }
    }
}

export class UserCheckUsernameResponse extends UcResponse
{
    constructor(rawData){
        super(rawData)
        let result = parseInt(rawData)
        if(isNaN(result)){
            result = 0
        }
        this.errCode = result
        if(result > 0){
            this.isSuccess = true
            this.errMsg = '用户名有效';
        }else{
            this.isSuccess = false;
            switch(result){
                case -1:
                    this.errMsg = '用户名不合法';
                    break;
                case -2:
                    this.errMsg = '包含不允许注册的词语';
                    break;
                case -3:
                    this.errMsg = '用户名已经存在'
                    break;
                default:
                    if(respRaw.length > 0){
                        this.errMsg = respRaw; // 如果响应的正文是文本信息，则直接作为错误消息
                    }else{
                        this.errMsg = '未知的错误！';
                    }
            }
        }
    }
}